package com.zz.xapp.interceptor

import android.text.TextUtils
import com.zz.framework.core.config.GlobalSetting
import com.zz.framework.core.ext.getVersion
import com.zz.framework.core.utils.language.MultiLanguages
import com.zz.xapp.App
import okhttp3.Interceptor
import java.util.*

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created on 2020/3/26 12:45 PM.
 * @version: V1.0
 */
object CommonParameterInterceptor {
    fun builder(): Interceptor {
        return Interceptor { chain ->
            val request = chain.request()
            val mBuilder = request.newBuilder()
            if (request.url.host.contains("api.nft.storage").not()) {
                mBuilder.addHeader("Authorization", if (TextUtils.isEmpty(GlobalSetting.getToken())) "" else "Bearer " + GlobalSetting.getToken())
            }
            mBuilder.addHeader("Accept-Language", if (MultiLanguages.getAppLanguage().language == Locale.ENGLISH.language) "en" else "zh") //多语言
            mBuilder.addHeader("Custom-Agent","Android")
            mBuilder.addHeader("Client-Platform","android")
            mBuilder.addHeader("x-tenant","cloud")
            mBuilder.addHeader("Client-Version", getVersion(App.context))
            val build = mBuilder.method(request.method, request.body).build()
            chain.proceed(build)
        }
    }
}